package mobi.hami.huapin.controller;

import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.config.AppConfig;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.AddressDetailDTO;
import mobi.hami.huapin.model.*;
import mobi.hami.huapin.service.*;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by 小峰 on 2018/8/23.
 */
@RestController
@RequestMapping(value = "/api/address", produces = "application/json")
@Api(description = "地址及收货人相关接口api")
public class AddressController {

    @Autowired
    private PoiInfoService poiInfoService;
    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserConsigneeService userConsigneeService;

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserAddressLogService userAddressLogService;

    @RequestMapping(value = "/getlist.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取详细信息列表", notes = "{adcode:poi的adcode,address:poi地址,city_name:省市区名称,id:poi的Id" +
            "latitude:纬度,longitude:经度,name:poi的名称}")
    public Result getAddressList(@RequestBody PoiInfo poiInfo) {
        PoiInfo poiInfo1 = poiInfoService.getOney(poiInfo.getId());
        List<AddressDetailDTO> addressDetailDTOList= new ArrayList<>();

        if (poiInfo1 != null && StringUtil.isNotEmpty(poiInfo1.getId())) {
            List<UserAddress>  userAddressList= userAddressService.getUserAddressByPoiId(poiInfo.getId());
            for (UserAddress useraddress:userAddressList){
                AddressDetailDTO addressDetailDTO= new AddressDetailDTO();
                addressDetailDTO.setPoiInfo(poiInfo1);
                addressDetailDTO.setUserAddress(useraddress);
                addressDetailDTOList.add(addressDetailDTO);
            }
        } else {
            poiInfoService.save(poiInfo);
        }
        return Results.successWithData(addressDetailDTOList, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/saveaddress.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存用户创建的详细信息", notes = "{detail_address:西店记忆－01,id: 0," +
            "poi_id:B000A816R6,user_id: 1}")
    public Result saveAddress(@RequestBody  UserAddress userAddress){
        int addressId=0;
        if(userAddress !=null && userAddress.getUser_id() !=null && userAddress.getPoi_id() !=null && userAddress.getDetail_address()!=null){
            addressId=userAddressService.save(userAddress);


            UserInfo userInfo= new UserInfo();
            userInfo.setId(userAddress.getUser_id());
            userInfo.setAddress_id(addressId);
            userInfoService.update(userInfo);

            UserAddressLog userAddressLog= new UserAddressLog();
            userAddressLog.setAddress_id(addressId);
            userAddressLog.setUser_id(userAddress.getUser_id());
            userAddressLogService.save(userAddressLog);
        }
        if(addressId>0)
        {
            return Results.successWithData(addressId,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else {
            return Results.successWithData("地址不存在",BaseEnums.ERROR.code(),BaseEnums.ERROR.desc());
        }

    }

    @RequestMapping(value = "/saveconsignee.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存收货人信息", notes = "{id:0,name:收货人名称,phone:收货人手机号" +
            "user_id:人员Id}")
    public Result saveUserConsignee(@RequestBody UserConsignee userConsignee){
        int consigneeId=0;
        if(userConsignee!=null && userConsignee.getId()>0)
        {
            consigneeId= userConsigneeService.update(userConsignee);
        }else {
            consigneeId=userConsigneeService.save(userConsignee);
        }
        UserInfo userInfo= new UserInfo();
        userInfo.setId(userConsignee.getUser_id());
        userInfo.setConsignee_id(consigneeId);
        userInfoService.update(userInfo);
        return Results.successWithData(consigneeId,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/addressbyuserid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户Id获取收货人详细信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户Id", example = "1", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result getAddressDetailByUserId(@RequestParam Integer userId){

        UserInfo userInfo=userInfoService.selectOnebyId(userId);
        UserAddress userAddress=userAddressService.selectOnebyId(userInfo.getAddress_id());
        UserConsignee userConsignee=userConsigneeService.selectOnById(userInfo.getConsignee_id());
        PoiInfo poiInfo= poiInfoService.getOney(userAddress.getPoi_id());

        AddressDetailDTO addressDetailDTO= new AddressDetailDTO();
        addressDetailDTO.setUserAddress(userAddress);
        addressDetailDTO.setUserConsignee(userConsignee);
        addressDetailDTO.setPoiInfo(poiInfo);
        return Results.successWithData(addressDetailDTO,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());

    }

    @RequestMapping(value = "/getprevoiusaddress.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户Id及地址Id获取上一次地址信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户Id", example = "1", paramType = "query", required = true, dataType = "Integer"),
                    @ApiImplicitParam(name = "addressId", value = "当前地址Id", example = "1", paramType = "query", required = true, dataType = "Integer")
            }
    )
    public Result getPrevoiusAddressInfo(@RequestParam Integer userId, Integer addressId)
    {
        Integer preaddressId=userAddressLogService.getPrevoiusAddressId(userId,addressId);
        if(preaddressId==null || preaddressId<=0){
            preaddressId=addressId;
        }
        UserAddress userAddress= userAddressService.selectOnebyId(preaddressId);
        PoiInfo poiInfo1 = poiInfoService.getOney(userAddress.getPoi_id());
        AddressDetailDTO addressDetailDTO= new AddressDetailDTO();
        addressDetailDTO.setPoiInfo(poiInfo1);
        addressDetailDTO.setUserAddress(userAddress);

        return  Results.successWithData(addressDetailDTO,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/getsaddressbyid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据地址Id获取地址详细信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "addressId", value = "当前地址Id", example = "1", paramType = "query", required = true, dataType = "Integer")
            }
    )
    public Result getAddressInfoByAddressId(Integer addressId){
            if(addressId ==null || addressId<=0)
            {
                AppConfig appConfig= new AppConfig();
                addressId=appConfig.getDefaultAddressId(); //给一个默认的值
            }
        UserAddress userAddress= userAddressService.selectOnebyId(addressId);
            if(userAddress==null || userAddress.getId()<=0){
                AppConfig appConfig= new AppConfig();
                addressId=appConfig.getDefaultAddressId(); //给一个默认的值
                userAddress= userAddressService.selectOnebyId(addressId);
            }
        PoiInfo poiInfo= poiInfoService.getOney(userAddress.getPoi_id());
        AddressDetailDTO addressDetailDTO= new AddressDetailDTO();
        addressDetailDTO.setUserAddress(userAddress);
        addressDetailDTO.setPoiInfo(poiInfo);
        return Results.successWithData(addressDetailDTO,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getnoteaddressInfo.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据四级获取地址详细信息(纸条地址)", notes = "{\"adcode\": \"110105\",\"address\": \"高碑店乡方家村甲18号\", \"city_name\": \"北京市朝阳区\",\n" +
            "  \"id\": \"B0FFGOO8LB\",\"latitude\": 116.51992, \"longitude\": 39.899371, \"name\": \"ITOWN·西店记忆\"}")
    @ApiResponses({
            @ApiResponse(code = 200,message="{addressName:空间名称,addressId:空间Id,NoteList:[{note_id:纸条Id,create_time:创建时间" +
                    ",note_comtent:纸条内容}],userNoteList:[{user_id:用户id,user_head:用户头像,note_count:气泡数}]}")
    })
    public Result getNoteAddressByPoiInfo(@RequestBody PoiInfo poiInfo){
        PoiInfo poiInfo1 = poiInfoService.getOney(poiInfo.getId());
        List<Map<String,Object>> userAddressList = new ArrayList<>();
        if (poiInfo1 != null && StringUtil.isNotEmpty(poiInfo1.getId())) {
            userAddressList= userAddressService.getNoteAddressByPoiId(poiInfo.getId());
            //userAddressList.put("poiInfo",poiInfo1);
        } else {
            poiInfoService.save(poiInfo);
        }
        return Results.successWithData(userAddressList,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
    }



}
